package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.ConsultCategoryQueryDTO;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.entity.ConsultCategory;
import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.cms.service.ConsultCategoryService;
import com.jeff.tianti.common.ColumnInfoCom;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/consultCategory")
public class ConsultCategoryController {
    @Autowired
    private ConsultCategoryService consultCategoryService;

    /**
     * @Date:_
     * @Ceator:liuYI
     * @Param:
     * @Return:
     * @Describe:用户添加资询类型
     */
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addConsult(HttpServletRequest request) {


        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");//主键id
            String name = request.getParameter("name");//名称
            String text = request.getParameter("text");//提示语
            String cmsId = request.getParameter("cmsId");//关联cms_column_info表id
            ConsultCategory consultCategory = null;
            if (StringUtils.isNotBlank(id)) {//修改
                consultCategory = consultCategoryService.find(id);
                consultCategory.setName(name);
                consultCategory.setText(text);
                consultCategoryService.update(consultCategory);
            } else {//保存
                consultCategory = new ConsultCategory();
                consultCategory.setName(name);
                consultCategory.setText(text);
                consultCategory.setCmsId(cmsId);
                consultCategoryService.save(consultCategory);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @Autowired
    private ColumnInfoService columnInfoService;

    /**
     * @Ceator:liuYI
     * @Describe:获取资询类型列表
     */
    @RequestMapping("/list")
    public String findList(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        String currentPageStr = request.getParameter("currentPage");//当前页
        String pageSizeStr = request.getParameter("pageSize");//每页显示条数
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        ConsultCategoryQueryDTO consultCategoryQueryDTO = new ConsultCategoryQueryDTO();
        if (StringUtils.isNotBlank(id)) {
            consultCategoryQueryDTO.setCmsId(id);
        }
        consultCategoryQueryDTO.setCurrentPage(currentPage);
        consultCategoryQueryDTO.setPageSize(pageSize);
        Map map = columnInfoCom.findColumnInfoList(Constants.COLUMNINFO_ZSCQ_CODE);
        PageModel<ConsultCategory> page = consultCategoryService.queryConsultCategoryPage(consultCategoryQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("id", id);
        model.addAttribute("map", map);
        model.addAttribute(Constants.MENU_NAME, Constants.INTELLECTUAL_PROPERTY);
        model.addAttribute("rootColumnId", Constants.COLUMNINFO_ZSCQ_CODE);
        return "user/consultCategory/consultCategory";
    }

    @Autowired
    private ColumnInfoCom columnInfoCom;
    /**
     * @Ceator:liuYI
     * @Describe:编辑资询类型属性
     */
    @RequestMapping("/edit")//编辑
    public String findById(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        ConsultCategory consultCategory = new ConsultCategory();
        if (StringUtils.isNotBlank(id)) {
            consultCategory = consultCategoryService.find(id);
        }
        Map map = columnInfoCom.findColumnInfoList( Constants.COLUMNINFO_ZSCQ_CODE);
        model.addAttribute("consultCategory", consultCategory);
        model.addAttribute("map", map);
        model.addAttribute(Constants.MENU_NAME, Constants.INTELLECTUAL_PROPERTY);
        return "user/consultCategory/consultCategory_edit";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String deleteFlag = request.getParameter("deleteFlag");
            if (StringUtils.isNotBlank(id)) {
                ConsultCategory consultCategory = consultCategoryService.find(id);
                consultCategory.setDeleteFlag(deleteFlag);
                consultCategoryService.update(consultCategory);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
